﻿using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.Core.Common;
using WebApi.Core.Model;
using WebApi.Core.Repository.Base;

namespace WebApi.Core.Repository.SqlSugar
{
    public static class DbContext//<T> where T : class, new()
    {
        private static readonly Lazy<ISqlSugarClient> _db = new Lazy<ISqlSugarClient>(()=>
        {
            var db = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = BaseDBConfig.ConnectionString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true, //开启自动释放模式
            });

            //// 配置日志
            //db.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    LoggerHelper.LogSql(sql, pars);
            //};

            return db;
        });

        public static ISqlSugarClient Db => _db.Value;

        //public SqlSugarClient Db; //用来处理事务多表查询和复杂的操作
        //public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } } //用来操作当前表的数据
        //public SimpleClient<Users> UserDb { get { return new SimpleClient<Users>(Db); } } //用来处理User表的常用操作

        //private readonly ILogger<DbContext<T>> _logger;
        //private string? _lastSql;
        //private object? _lastParameters;

        //public DbContext(ILogger<DbContext<T>> logger) 
        //public DbContext()
        //{
            //_logger = logger;

            //Db = new SqlSugarClient(new ConnectionConfig()
            //{
            //    ConnectionString = BaseDBConfig.ConnectionString,
            //    DbType = DbType.SqlServer,
            //    InitKeyType = InitKeyType.Attribute, //从特性读取主键和自增列信息
            //    IsAutoCloseConnection = true, //开启自动释放模式
            //});
            //Db.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    Console.WriteLine(sql + "\r\n" +
            //        Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            //    Console.WriteLine();
            //}; 
            //Db.Aop.OnLogExecuted = (sql, pars) =>
            //{
            //    _lastSql = sql;
            //    _lastParameters = pars.ToDictionary(p => p.ParameterName, p => p.Value);

            //    var logMessage = $"{sql}\n参数：{Db.Utilities.SerializeObject(pars.ToDictionary(p => p.ParameterName, p => p.Value))}";
            //    _logger.LogDebug(logMessage); // 使用 ILogger 记录
            //};

            //Db.Aop.OnError = ex =>
            //{
            //    _logger.LogError(ex, $"SQL执行失败，SQL：{_lastSql}\n参数：{_lastParameters}");
            //};
            //Db = CreateClient();
        //}
        //public SqlSugarClient CreateClient()
        //{ 
        //    return new SqlSugarClient(new ConnectionConfig()
        //    {
        //        ConnectionString = BaseDBConfig.ConnectionString,
        //        DbType = DbType.SqlServer,
        //        InitKeyType = InitKeyType.Attribute, //从特性读取主键和自增列信息
        //        IsAutoCloseConnection = true, //开启自动释放模式
        //    });
        //}
    }
}
